package com.group4.admin.beans;

import com.group4.controller.AdminFacade;
import com.group4.entity.Admin;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author anjonas, andake
 */
@ManagedBean
@RequestScoped
public class IndexControllerBean {

    @EJB
    AdminFacade adb;
    @ManagedProperty(value = "#{indexBackingBean}")
    private IndexBackingBean ib;

    /** Creates a new instance of IndexControllerBean */
    public IndexControllerBean() {
    }

    public IndexBackingBean getIb() {
        return ib;
    }

    public void setIb(IndexBackingBean ib) {
        this.ib = ib;
    }

    public String login() {

        Admin admin = adb.findByLogin(ib.getUsername(), ib.getPassword());
        
        if (admin != null) {
            HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
            session.setAttribute("user", admin);
            return "successLogin";
        } else {
            return "failedLogin";
        }
    }
}